package lms.dao.impl;

import lms.dao.ManagerDao;
import lms.entity.Admin;
import lms.utils.DBUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @description: Dao层实现类
 * @author: Grape_Pip
 */
public class ManagerDaoImpl implements ManagerDao {
    /**
     * 获取JDBCTemplate对象
     **/
    private JdbcTemplate template = new JdbcTemplate(DBUtils.getDataSource());
    /**
     * 数据库连接对象
     */
    Connection conn = null;
    /**
     * 数据库操作对象
     */
    PreparedStatement pstmt = null;
    /**
     * 结果集
     */
    ResultSet rs = null;

    @Override
    public Admin mLogin(Admin adminInfo) {
        try {

            String sql = "select * from l_admin where mname = ? and mpwd = ?";
            return template.queryForObject(sql, new BeanPropertyRowMapper<Admin>(Admin.class), adminInfo.getManagerName(), adminInfo.getManagerpPwd());
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public int mRegister(String adminRName, String adminRPwd) {
        try {

            String sql = "insert into l_admin (`mname`,`mpwd`) " +
                    "VALUES (?,?)";
            int res = template.update(sql, adminRName,adminRPwd);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
